
import com.sun.source.tree.Tree;
import org.w3c.dom.Node;

import java.util.*;

public class BinaryTree {
     class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    private TreeNode root;

    public TreeNode createTree(){
        TreeNode node1 = new TreeNode('A');
        TreeNode node2 = new TreeNode('B');
        TreeNode node3 = new TreeNode('C');
        TreeNode node4 = new TreeNode('D');
        TreeNode node5 = new TreeNode('E');
        TreeNode node6 = new TreeNode('F');
        TreeNode node7 = new TreeNode('G');

        root = node1;
        node1.left = node2;
        node2.left = node3;
        node2.right = node7;
        node1.right = node4;
        node4.left = node5;
        node4.right = node6;

        return root;

    }

    // 前序遍历--根->左子树->右子树
    public void preOrder(TreeNode root){

        if(root == null){
            return;
        }

        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void preOrderNotRecursion(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            if(top.right != null){
                cur = top.right;
            }
        }
    }

    //前序遍历的非递归实现
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> ret = new LinkedList<>();
        if(root == null){
            return ret;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                ret.add(cur.val);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            if(top.right != null){
                cur = top.right;
            }
        }

        return ret;
    }

    //leetCode练习
    //子问题思路
    /* List<Integer> list = new LinkedList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null){
            return list;
        }

        list.add(root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        list.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.right);
        list.addAll(rightTree);
        return list;
    }

    //遍历思路
    List<Integer> list = new LinkedList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
            if(root == null){
                return list;
            }

            list.add(root.val);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
            return list;
        } */




    // 中序遍历--左子树->根->右子树
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public void inOrderNotRecursion(TreeNode root) {
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            if(top.right != null){
                cur = top.right;
            }
        }
    }

    //中序遍历的非递归实现
    public List<Character> inorderTraversal(TreeNode root) {
        List<Character> ret = new LinkedList<>();
        if(root == null){
            return ret;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode pop = stack.pop();
            ret.add(pop.val);
            if(pop.right != null){
                cur = pop.right;
            }
        }

        return ret;

    }

    // 后序遍历--左子树->根->右子树
   public void postOrder(TreeNode root){
        if(root == null){
            return;
        }

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //后序遍历的非递归实现
    public void postOrderNotRecursion(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || prev == top.right){
                stack.pop();
                System.out.print(top.val + " ");
                prev = top;
            }else {
                cur = top.right;
            }
        }
    }

    public List<Character> postorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev){
                 stack.pop();
                 char val = top.val;
                ret.add(val);
                prev = top;
            }else {
                cur = top.right;
            }
        }

        return ret;

    }



    // 获取树中节点的个数
    //遍历
   // public static int nodeSize = 0;
   // public void size(TreeNode root){
   //     if(root == null){
   //         return;
   //     }
   //
   //     nodeSize++;
   //     size(root.left);
   //     size(root.right);
   //     // return nodeSize;
   //  }

    //子问题
    public int size2(TreeNode root){
        if(root == null){
            return 0;
        }

        return (size2(root.left) + size2(root.right) + 1);
    }

    // 获取叶子节点的个数
    //遍历思路
    // public static int leafNode = 0;
    // public void getLeafNodeCount(TreeNode root){
    //     if(root == null){
    //         return;
    //     }
    //     if(root.right == null && root.left == null){
    //         leafNode++;
    //     }
    //
    //     getLeafNodeCount(root.left);
    //     getLeafNodeCount(root.right);
    // }

    //子问题
    public int getLeafNodeCount2(TreeNode root){
        if(root == null){
            return 0;
        }

        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }


    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return (getKLevelNodeCount(root.left,k - 1) +
                getKLevelNodeCount(root.right,k - 1));
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }

        find(root.left,val);
        find(root.right,val);

        return null;
    }

    // 获取二叉树的高度
    //时间复杂度：o(N)
    //空间复杂度：O(logN)
    int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }

        int left = getHeight(root.left);
        int right = getHeight(root.right);

        return Math.max(left,right) + 1;
    }

    //层序遍历
    public void levelOrder(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while(size != 0){
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

    // 判断一棵树是不是完全二叉树
    public  boolean isCompleteTree(TreeNode root){
        if(root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while (!queue.isEmpty()){
            TreeNode peek = queue.peek();
            if(peek != null){
                return false;
            }
            queue.poll();
        }
        return true;
    }
    public boolean isCompleteTree2(TreeNode root){
        if(root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(queue.peek() != null){
           TreeNode cur = queue.poll();
           if(cur == null){
               break;
           }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        //检查队列中是否全是null
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            TreeNode cur = queue.poll();
            if(cur != null){
                return false;
            }
        }
        return true;
    }

    //根据二叉树创造字符串
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder = new StringBuilder();
        tree2strChild(root,stringBuilder);
        return stringBuilder.toString();
    }

    public void tree2strChild(TreeNode root,StringBuilder stringBuilder){
        if(root == null){
            return;
        }
        stringBuilder.append(root.val);
        if(root.left != null){
            stringBuilder.append("(");
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(")");
        }else {
            if(root.right != null){
                stringBuilder.append("()");
            }else {
                return;
            }
        }

        if(root.right != null){
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(")");
        }else {
            return;
        }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {

            return root;
        }

        if(root.val == p.val || root.val == q.val){
            return root;
        }

        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);

        if(leftTree != null && rightTree != null){
            return root;
        }else if(leftTree != null){
            return leftTree;
        }else{
            return rightTree;
        }
    }

    //二叉树的层序遍历II
    public List<List<Character>> levelOrderBottom(TreeNode root) {
        List<List<Character>> levelOrder = new ArrayList<>();
        if(root == null){
            return levelOrder;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Character> list = new LinkedList<>();
            while(size != 0){
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            levelOrder.add(list);
        }

        //Collections.reverse(levelOrder);

        List<List<Character>> ret = new ArrayList<>();
        for (int i = levelOrder.size() - 1; i >= 0 ; i--) {
            ret.add(levelOrder.get(i));
        }

        return ret;

    }


}
